package com.farm.kaiping.controller;


import com.farm.kaiping.entity.Pigpen;
import com.farm.kaiping.po.PigpenPO;
import com.farm.kaiping.service.PigpenLogicService;
import com.farm.kaiping.service.PigpenService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 圈舍管理表(Pigpen)表控制层
 *
 * @author makejava
 * @since 2025-01-22 13:58:16
 */
@RestController
@RequestMapping("/pigpen")
@AllArgsConstructor
@Api(tags = "圈舍管理表控制器")
public class PigpenController {

	private final PigpenService pigpenService;
	private final PigpenLogicService pigpenLogicService;

	@ApiOperation(value = "分页查询圈舍管理表", notes = "权限：view_pigpen_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pigpen_page')")
	@PostMapping("/page")
	public R<Page<Pigpen>> getPigpenPage(@RequestBody PigpenPO pigpenPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(pigpenPO);
		return R.ok(pigpenService.getPigpenPage(page, pigpenPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询圈舍管理表列表", notes = "权限：view_pigpen_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pigpen_list')")
	@PostMapping("/list")
	public R<List<PigpenPO>> getPigpenList(@RequestBody PigpenPO pigpenPO) {
		try{
			return R.ok(pigpenLogicService.getPigpenList(pigpenPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询圈舍管理表", notes = "权限 view_pigpen_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pigpen_view')" )
	public R<Pigpen> getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(pigpenService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条圈舍管理表", notes = "权限：view_pigpen_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pigpen_one')")
	@PostMapping("/one")
	public R<Pigpen> getPigpen(@RequestBody PigpenPO pigpenPO) {
		try{
			return R.ok(pigpenService.getPigpen(pigpenPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增圈舍管理表", notes = "权限：view_pigpen_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pigpen_add')")
	@PostMapping("/add")
	public R<Integer> savePigpen(@RequestBody PigpenPO pigpenPO) {
		try{
			pigpenLogicService.checkBeforeSave(pigpenPO);
		    return R.ok(pigpenService.savePigpen(pigpenPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改圈舍管理表", notes = "权限：view_pigpen_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pigpen_edit')")
	@PostMapping("/edit")
	public R<String> updatePigpen(@RequestBody PigpenPO pigpenPO) {
		try{
		    return R.ok(pigpenService.updatePigpen(pigpenPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除圈舍管理表", notes = "权限：view_pigpen_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pigpen_del')")
	@PostMapping("/del")
	public R<String> removePigpen(@RequestBody PigpenPO pigpenPO) {
		try{
			pigpenLogicService.checkBeforeRemove(pigpenPO);
		    return R.ok(pigpenService.removePigpen(pigpenPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}
	
}

